home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / deskbar-applet / handlers / files.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  11.1 KB  |  293 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import os
  5. import urllib
  6. import sys
  7. import cgi
  8. import ConfigParser
  9. from os.path import join, basename, normpath, abspath, dirname
  10. from os.path import split, expanduser, exists, isfile, isdir
  11. from gettext import gettext as _
  12. import gobject
  13. import gtk
  14. import gnome
  15. import gnome.ui as gnome
  16. import gnomevfs
  17. import deskbar
  18. import deskbar.Indexer as deskbar
  19. import deskbar.Handler as deskbar
  20. import deskbar.Match as deskbar
  21. from deskbar.defs import VERSION
  22. from deskbar.Watcher import FileWatcher
  23. from deskbar.Utils import spawn_async, url_show_file
  24. HANDLERS = {
  25.     'FileFolderHandler': {
  26.         'name': _('Files, Folders and Places'),
  27.         'description': _('View your files, folders, bookmarks, drives, network places by name'),
  28.         'version': VERSION } }
  29. NETWORK_URIS = [
  30.     'http',
  31.     'ftp',
  32.     'smb',
  33.     'sftp']
  34. AUDIO_URIS = [
  35.     'cdda']
  36. MONITOR = gnomevfs.VolumeMonitor()
  37. GTK_BOOKMARKS_FILE = expanduser('~/.gtk-bookmarks')
  38.  
  39. class FileMatch(deskbar.Match.Match):
  40.     
  41.     def __init__(self, backend, name = None, absname = None, **args):
  42.         deskbar.Match.Match.__init__(self, backend, name = name, **args)
  43.         self._icon = deskbar.Utils.load_icon_for_file(absname)
  44.         self.absname = absname
  45.  
  46.     
  47.     def action(self, text = None):
  48.         url_show_file(self.absname)
  49.  
  50.     
  51.     def is_valid(self, text = None):
  52.         return exists(self.absname)
  53.  
  54.     
  55.     def get_category(self):
  56.         return 'files'
  57.  
  58.     
  59.     def get_verb(self):
  60.         return _('Open %s') % '<b>%(name)s</b>'
  61.  
  62.     
  63.     def get_hash(self, text = None):
  64.         return self.absname
  65.  
  66.  
  67.  
  68. class FolderMatch(deskbar.Match.Match):
  69.     
  70.     def __init__(self, backend, name = None, absname = None, **args):
  71.         deskbar.Match.Match.__init__(self, backend, name = name, **args)
  72.         self._icon = deskbar.Utils.load_icon_for_file(absname)
  73.         self.absname = absname
  74.  
  75.     
  76.     def action(self, text = None):
  77.         url_show_file(self.absname)
  78.  
  79.     
  80.     def is_valid(self, text = None):
  81.         return exists(self.absname)
  82.  
  83.     
  84.     def get_category(self):
  85.         return 'places'
  86.  
  87.     
  88.     def get_verb(self):
  89.         return _('Open folder %s') % '<b>%(name)s</b>'
  90.  
  91.     
  92.     def get_hash(self, text = None):
  93.         return self.absname
  94.  
  95.  
  96.  
  97. class GtkBookmarkMatch(deskbar.Match.Match):
  98.     
  99.     def __init__(self, backend, name = None, path = None, **args):
  100.         deskbar.Match.Match.__init__(self, backend, name = name, **args)
  101.         self.path = path
  102.  
  103.     
  104.     def action(self, text = None):
  105.         spawn_async([
  106.             'nautilus',
  107.             self.path])
  108.  
  109.     
  110.     def is_valid(self, text = None):
  111.         return exists(self.path)
  112.  
  113.     
  114.     def get_category(self):
  115.         return 'places'
  116.  
  117.     
  118.     def get_verb(self):
  119.         return _('Open location %s') % '<b>%(name)s</b>'
  120.  
  121.     
  122.     def get_hash(self, text = None):
  123.         return self.path
  124.  
  125.  
  126.  
  127. class VolumeMatch(deskbar.Match.Match):
  128.     
  129.     def __init__(self, backend, name = None, drive = None, icon = None):
  130.         deskbar.Match.Match.__init__(self, backend, name = name, icon = icon)
  131.         self.drive = drive
  132.  
  133.     
  134.     def action(self, text = None):
  135.         if self.drive == None:
  136.             spawn_async([
  137.                 'nautilus'])
  138.         else:
  139.             spawn_async([
  140.                 'nautilus',
  141.                 self.drive])
  142.  
  143.     
  144.     def is_valid(self, text = None):
  145.         return exists(self.drive)
  146.  
  147.     
  148.     def get_category(self):
  149.         return 'places'
  150.  
  151.     
  152.     def get_verb(self):
  153.         if self.drive == None:
  154.             uri_scheme = None
  155.         else:
  156.             uri_scheme = gnomevfs.get_uri_scheme(self.drive)
  157.         if uri_scheme in NETWORK_URIS:
  158.             return _('Open network place %s') % '<b>%(name)s</b>'
  159.         elif uri_scheme in AUDIO_URIS:
  160.             return _('Open audio disc %s') % '<b>%(name)s</b>'
  161.         else:
  162.             return _('Open location %s') % '<b>%(name)s</b>'
  163.  
  164.     
  165.     def get_hash(self, text = None):
  166.         return self.drive
  167.  
  168.  
  169.  
  170. class FileFolderHandler(deskbar.Handler.Handler):
  171.     
  172.     def __init__(self):
  173.         deskbar.Handler.Handler.__init__(self, gtk.STOCK_OPEN)
  174.         self._locations = { }
  175.  
  176.     
  177.     def initialize(self):
  178.         if not hasattr(self, 'watcher'):
  179.             self.watcher = FileWatcher()
  180.             self.watcher.connect(('changed',), (lambda watcher, f: self._scan_bookmarks_files()))
  181.         
  182.         self.watcher.add(GTK_BOOKMARKS_FILE)
  183.         self._scan_bookmarks_files()
  184.  
  185.     
  186.     def stop(self):
  187.         self.watcher.remove(GTK_BOOKMARKS_FILE)
  188.  
  189.     
  190.     def query(self, query):
  191.         result = []
  192.         result += self.query_filefolder(query, False)
  193.         result += self.query_filefolder(query, True)
  194.         query = query.lower()
  195.         for name, loc in self._locations.items():
  196.             if bmk.startswith(query):
  197.                 result.append(GtkBookmarkMatch(self, name, loc))
  198.                 continue
  199.         
  200.         for drive in MONITOR.get_mounted_volumes() + MONITOR.get_connected_drives():
  201.             if not drive.is_user_visible():
  202.                 continue
  203.             
  204.             if not drive.is_mounted():
  205.                 continue
  206.             
  207.             if not drive.get_display_name().lower().startswith(query):
  208.                 continue
  209.             
  210.             result.append(VolumeMatch(self, drive.get_display_name(), drive.get_activation_uri(), drive.get_icon()))
  211.         
  212.         return result
  213.  
  214.     
  215.     def query_filefolder(self, query, is_file):
  216.         (completions, prefix, relative) = filesystem_possible_completions(query, is_file)
  217.  
  218.     
  219.     def _scan_bookmarks_files(self):
  220.         if not isfile(GTK_BOOKMARKS_FILE):
  221.             return None
  222.         
  223.         for line in file(GTK_BOOKMARKS_FILE):
  224.             line = line.strip()
  225.             
  226.             try:
  227.                 if gnomevfs.exists(line):
  228.                     uri = urllib.unquote(line)
  229.                     (head, tail) = split(uri)
  230.                     if tail == '':
  231.                         i = head.rfind('/')
  232.                         tail = head[i + 1:]
  233.                     
  234.                     self._locations[tail.lower()] = (tail, line)
  235.             continue
  236.             except Exception:
  237.                 msg = None
  238.                 print 'Error:_scan_bookmarks_files:', msg
  239.                 continue
  240.             
  241.  
  242.         
  243.  
  244.  
  245.  
  246. def filesystem_possible_completions(prefix, is_file = False):
  247.     '''
  248. \tGiven an path prefix, retreive the file/folders in it.
  249. \tIf files is False return only the folder, else return only the files.
  250. \tReturn a tuple (list, prefix, relative)
  251. \t  list is a list of files whose name starts with prefix
  252. \t  prefix is the prefix effectively used, and is always a directory
  253. \t  relative is a flag indicating wether the given prefix was given without ~ or /
  254. \t'''
  255.     relative = False
  256.     if not prefix.startswith('~') and not prefix.startswith('/'):
  257.         relative = True
  258.         prefix = join('~/', prefix)
  259.     
  260.     if prefix.startswith('~') and not prefix.startswith('~/') and len(prefix) > 1:
  261.         prefix = join('~/', prefix[1:])
  262.     
  263.     if prefix.endswith('/'):
  264.         prefix = prefix[:-1]
  265.     
  266.     if prefix == '~':
  267.         return ([
  268.             expanduser(prefix)], dirname(expanduser(prefix)), relative)
  269.     
  270.     start = None
  271.     path = normpath(abspath(expanduser(prefix)))
  272.     (prefix, start) = split(prefix)
  273.     path = normpath(abspath(expanduser(prefix)))
  274.     if not exists(path):
  275.         return ([], prefix, relative)
  276.     
  277.     if my_isfile(path):
  278.         print 'Myisfile:', is_file
  279.         if is_file:
  280.             return ([
  281.                 path], dirname(prefix), relative)
  282.         else:
  283.             return ([], prefix, relative)
  284.     
  285.     return (_[1], prefix, relative)
  286.  
  287.  
  288. def my_isfile(path):
  289.     if isfile(path):
  290.         pass
  291.     return not path.endswith('.savedSearch')
  292.  
  293.